package com.yuzhenxin.coursesystem.controller.course;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.framework.common.pojo.R;
import com.yuzhenxin.coursesystem.config.EndPoints;
import com.yuzhenxin.coursesystem.controller.course.vo.CourseCreateReqVO;
import com.yuzhenxin.coursesystem.controller.course.vo.CourseQueryReqVO;
import com.yuzhenxin.coursesystem.entity.CourseDO;
import com.yuzhenxin.coursesystem.listener.CourseListener;
import com.yuzhenxin.coursesystem.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Tag(name = "课程管理")
@RestController
public class CourseController {

    @Resource
    private CourseService courseService;

    @Operation(summary = "根据课程号获取课程信息")
    @GetMapping(EndPoints.COURSE_GETBYID)
    public R<CourseDO> getById(@RequestParam("id") Integer id) {
        CourseDO courseEntity = courseService.getById(id);
        return R.success(courseEntity);
    }

    @Operation(summary = "分页条件查询所有课程信息")
    @PostMapping(value = EndPoints.COURSE_SELECTPAGE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<IPage<CourseDO>> selectPage(@Valid @RequestBody CourseQueryReqVO reqDto) {
        IPage<CourseDO> courseIPage = courseService.selectPage(reqDto);
        return R.success(courseIPage);
    }

    @Operation(summary = "查看课程总量")
    @GetMapping(EndPoints.COURSE_SELECTCOUNT)
    public R<Long> selectCount() {
        long count = courseService.count();
        return R.success(count);
    }

    @Operation(summary = "根据学分划分课程")
    @GetMapping(EndPoints.COURSE_SELECTCOURSEGROUPBYCREDIT)
    public R<List<Map<String, Object>>> selectCourseGroupByCcredit() {
        List<Map<String, Object>> list = courseService.selectCourseGroupByCredit();
        return R.success(list);
    }

    @Operation(summary = "导出数据")
    @GetMapping(EndPoints.COURSE_EXPORTDATA)
    public void exportData(HttpServletResponse response) {
        courseService.exportData(response);
    }

    @Operation(summary = "添加课程信息")
    @PostMapping(value = EndPoints.COURSE_ADD, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> insert(@Valid @RequestBody CourseCreateReqVO reqVO) {
        courseService.save(CourseDO.builder()
                .number(reqVO.getNumber())
                .name(reqVO.getName())
                .credit(reqVO.getCredit())
                .build());
        return R.success();
    }

    @Operation(summary = "添加批量课程")
    @PostMapping(value = EndPoints.COURSE_IMPORTDATA)
    public R<?> importData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), CourseDO.class, new CourseListener(courseService)).sheet().doRead();
        return R.success();
    }

    @Operation(summary = "修改课程信息")
    @PutMapping(value = EndPoints.COURSE_UPDATEBYID, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> updateById(@RequestBody CourseDO courseDO) {
        courseService.updateById(courseDO);
        return R.success();
    }

    @Operation(summary = "删除单个课程信息")
    @DeleteMapping(EndPoints.COURSE_REMOVEBYID)
    public R<?> removeById(@RequestParam("id") Integer id) {
        courseService.removeById(id);
        return R.success();
    }

    @Operation(summary = "批量删除多个课程信息")
    @DeleteMapping(value = EndPoints.COURSE_REMOVEBYIDS, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> removeByIds(@RequestBody List<Integer> idList) {
        courseService.removeByIds(idList);
        return R.success();
    }

}
